package com.tanhua.server.service;

import com.tanhua.db.Announcement;
import com.tanhua.dubbo.api.AnnouncementsApi;
import com.tanhua.vo.AnnouncementVo;
import com.tanhua.vo.PageResult;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * @version 1.0
 * @author： xtr
 * @date： 2021-03-10 17:26
 */
@Service
public class AnnouncementsService {

    @Reference
    private AnnouncementsApi announcementsApi;
    /**
     * 公告列表
     * @param page
     * @param pagesize
     * @return
     */
    public PageResult getAnnouncement(long page, long pagesize) {
        PageResult<Announcement> pageResult = announcementsApi.getAnnouncement(page,pagesize);
        List<Announcement> items = pageResult.getItems();
        PageResult result = null;
        if (items!=null){
            List<AnnouncementVo> listVo = new ArrayList<>();
            for (Announcement item : items) {
                AnnouncementVo announcementVo = new AnnouncementVo();
                BeanUtils.copyProperties(item,announcementVo);
                if (item.getCreated()!=null){
                    announcementVo.setCreateDate(new SimpleDateFormat("yyyy-MM-dd hh:mm").format(item.getCreated()));
                }
                listVo.add(announcementVo);
            }
            result = new PageResult(pageResult.getCounts(),pageResult.getPagesize(),pageResult.getPages(),pageResult.getPage(),listVo);
        }
        return result;
    }
}
